import 'package:flutter/material.dart';
import 'package:body_measurement/common/measuring_api.dart';
import 'package:body_measurement/models/measuring_task.dart';
import 'package:body_measurement/models/task_data_source.dart';
import 'package:body_measurement/widgets/aside_nav.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static const List<String> _taskFilterList = <String>['全部', '已量体', '未量体'];

  @override
  void initState() {
    MeasuringApi().getTaskList(Provider.of<TaskDataSource>(context, listen: false));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(toolbarHeight: 0),
      body: Row(
        children: <Widget>[
          AsideNav(ModalRoute.of(context)?.settings.arguments as String),
          Expanded(
            flex: 1,
            child: Container(
              width: double.infinity,
              color: const Color(0xffededed),
              child: Column(
                children: <Widget>[
                  // _buildActionBar(),
                  _buildTaskTable(context),
                ],
              ),
            ),
          ),
        ],
      ),
      backgroundColor: Colors.lightBlue[50],
    );
  }

  Widget _buildActionBar() {
    return SizedBox(
      width: double.infinity,
      height: 60.0,
      child: Row(
        children: <Widget>[
          _buildSearchBox(),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 5.0),
            child: ElevatedButton(
              onPressed: () {
                Navigator.of(context).pushNamed('task_detail_page', arguments: {'task_id': 107});
              },
              child: const Text('新建任务'),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 5.0),
            child: ElevatedButton(
              onPressed: () {},
              child: const Text('删除任务'),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildTaskTable(BuildContext context) {
    return Expanded(
      flex: 1,
      child: SingleChildScrollView(
        padding: const EdgeInsets.all(4.0),
        child: SizedBox(
          child: Consumer<TaskDataSource>(
            builder: (context, value, child) => PaginatedDataTable(
              header: const Text('量体任务表'),
              actions: <Widget>[
                _buildSearchBox(),
                IconButton(onPressed: () {}, icon: const Icon(Icons.add, color: Colors.black)),
                IconButton(onPressed: () {}, icon: const Icon(Icons.delete, color: Colors.black)),
              ],
              columns: const [
                DataColumn(label: Text('序号')),
                DataColumn(label: Text('任务名称')),
                DataColumn(label: Text('状态')),
                DataColumn(label: Text('配置')),
                DataColumn(label: Text('量体师')),
                DataColumn(label: Text('量体部位')),
                DataColumn(label: Text('创建人')),
                DataColumn(label: Text('操作')),
              ],
              // rowsPerPage: 8,
              source: value,
            ),
          ),
        ),
      ),
    );
  }

  // 搜索框
  Widget _buildSearchBox() {
    return const SizedBox(
      width: 220,
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 10.0),
        child: Expanded(
          child: TextField(
            decoration: InputDecoration(
              hintText: '请输入搜索关键词',
              prefixIcon: Icon(Icons.search),
              border: OutlineInputBorder(),
            ),
            keyboardType: TextInputType.text,
          ),
        ),
      ),
    );
  }
}
